package be.bonamis.umpire.old.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import be.bonamis.umpire.old.domain.CsvDispoArbitres;
import be.bonamis.umpire.old.domain.XlsCaUmpire;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

public class ImportData {
	public static List<CsvDispoArbitres> getOldDbUmpiresList(String filename) {
		ColumnPositionMappingStrategy<CsvDispoArbitres> strat = new ColumnPositionMappingStrategy<CsvDispoArbitres>();
		strat.setType(CsvDispoArbitres.class);
		String[] columns = new String[] { "id", "nomArbitre", "prenomArbitre",
				"emailArbitre", "typeArbitre", "classment", "login", "passwd",
				"admin" }; // the fields to bind do in your JavaBean
		strat.setColumnMapping(columns);

		CsvToBean<CsvDispoArbitres> csv = new CsvToBean<CsvDispoArbitres>();
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		URL url = classLoader.getResource(filename);
		File inputWorkbook = null;
		CSVReader csvReader = null;
		try {
			inputWorkbook = new File(url.toURI().getPath());
			csvReader = new CSVReader(new FileReader(inputWorkbook));
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return csv.parse(strat, csvReader);
	}

	public static XlsCaUmpire getXlsCaUmpirebyId(List<XlsCaUmpire> list,
			final int idToFind) {

		return Iterables.find(list, new Predicate<XlsCaUmpire>() {
			public boolean apply(XlsCaUmpire umpire) {
				return Integer.parseInt(umpire.getOLdId()) == idToFind;
			}
		}, null);
	}
}
